﻿using System.Collections.Generic;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Interfaces.Services;
using Netkk.Common.Models;
using System;
using System.Linq;

namespace Netkk.Library.Services
{
    public class LinkService : ILinkService
    {
        private readonly ILinkRepository repository;

        public LinkService(ILinkRepository repository)
        {
            this.repository = repository;
        }

        public IEnumerable<Link> GetAllLinks()
        {
            return repository.Select();
        }

        public IEnumerable<Link> GetAllLinksFromCategory(string category)
        {
            IEnumerable<Link> alllinks = repository.Select();
            IEnumerable<Link> links = from l in alllinks
                                      where l.Category.Equals(category, StringComparison.OrdinalIgnoreCase)
                                      select l;
            return links;
        }

        public Link GetLinkById(Guid id)
        {
            IEnumerable<Link> links = repository.Select();
            Link link = (from l in links
                         where l.Id == id
                         select l).FirstOrDefault();
            return link;
        }

        public bool SaveLink(Link link)
        {
            try
            {
                repository.Save(link);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public void DeleteLink(Link link)
        {
            repository.Delete(link);
        }

    }

}
